package xin.alum.aim.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class LengthDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        int length = in.readInt();
        //发生断包情况，等待接收完成
        if (in.readableBytes() < length) {
            in.resetReaderIndex();
            return;
        } else {
            ByteBuf proto = in.readRetainedSlice(length);
            ctx.fireChannelRead(proto);
        }
    }
}
